<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller 
{
    private $appid = 'wxb39298479fdd6af3';//养悦源公众号id

    private $secret = 'ab0743696ece96b5e3ea3ad042c565a2';//养悦源公众号密匙

    private $wechatRedirect = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb39298479fdd6af3&redirect_uri=http://www.yangyueyuan.com&response_type=code&scope=snsapi_base#wechat_redirect';

    //通用入口
	public function index()
	{
		parent::__construct();
        $this->load->library('session');
        $code = $this->input->get('code');
        $locationid = $this->input->get('locationid');//地址id
        $isWeChat = $this->isWeChat();
        if($isWeChat && isset($code))
        {
            //微信授权回调
            $from = $this->input->get('from');
            $user = $this->webAccessToken($code, $this->appid, $this->secret);
            $this->session->set_userdata('openid', $user->openid);
            if($from == 1)
            {
                //待付款订单详情微信授权回调
                $order_id = $this->input->get('order_id');
                header('Location:' . $this->config->item('base_url') . 'home/order/orderinfo?orderid=' . $order_id);
            }
            else if($from == 2)
            {
                //提交订单页面微信授权回调
                $data = $this->input->get('data');
                header('Location:' . $this->config->item('base_url') . 'home/order/submitorder?data=' . $data);
            }
            else
            {
                header('Location:'.$this->config->item('base_url').'home/goods');
            }
        }
        else
        {
            //正常扫码进入
            $this->addscan($locationid);
            header('Location:'.$this->config->item('base_url').'home/goods');
        }
	}

    //添加访问记录
    private function addscan($locationid)
    {
        if(!empty($locationid)){
            $this->load->model('Qrcode_model');
            $params['location_id'] = $locationid;
            $params['update_time'] = time();
            $params['number'] = 1;
            $this->Qrcode_model->add($params);
        }
    }

	//获取临时access_token
	private function webAccessToken($code,$appid,$secret)
    {
        return $this->http_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code');
    }
    //curl请求
    private function http_curl($url, $type = 'GET', $data = null, $settings = null)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        is_null($data) or curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // $data为数组
        !isset($settings['timeout']) or curl_setopt($ch, CURLOPT_TIMEOUT, $settings['timeout']);
        $response = curl_exec($ch);
        if ($error = curl_error($ch)) {
            throw new Exception('http_curl error: ' . $error);
        }
        curl_close($ch);
        return empty($settings['rawdata']) ? json_decode($response) : $response;
    }

    /**
     * 根据useragent判断是否是微信客户端
     * @return boolean [description]
     */
    private function isWeChat()
    {
        $userAgent = $_SERVER['HTTP_USER_AGENT'];
        if(strpos(strtolower($userAgent), 'micromessenger') !== false)
        {
            return true;
        }
        return false;
    }
}
